﻿using CozeDomain.Entities;
using CozeDomain.IRepositories;
using CozeDto.ConsumptionHistoryDtos;
using CozeDto.ConversationDtos;
using CozeDto.RechargeHistoryDtos;
using CozeDto.SaleHistoryDtos;
using D.UtilFreesqlOrm;


namespace CozeDomain.Repositories
{
    public class ConversationEntityRepository : BaseRepository<ConversationEntity>, IConversationEntityRepository
    {
        public ConversationEntityRepository(IFreeSql db) : base(db)
        {
        }

        public async Task<List<RepConversationInfo>> QueryConversationListAsync(string userId)
        {
            return await _db.Select<ConversationEntity, ProjectEntity>()
                                                            .LeftJoin((a, b) => a.ProjectId == b.Id)
                                                            .Where((a, b) => a.UserId == userId)
                                                            .ToListAsync((a, b) => new RepConversationInfo
                                                            {
                                                                Id = a.Id,
                                                                ProjectId = b.Id,
                                                                CreateTime = a.CreateTime,
                                                                Icon = b.Icon,
                                                                Name = b.Name,
                                                                Title = a.Title,
                                                                ConversationId = a.ConversationId,
                                                                ChatId = a.ChatId,
                                                            });
        }

        public async Task<Tuple<long, List<RepConversationInfo>>> QuerySaleHistoryPagesAsync(ReqQueryConversationPage reqQuery)
        {
            var where = _db.Select<ConversationEntity, ProjectEntity>()
                                                            .LeftJoin((a, b) => a.ProjectId == b.Id)
                                                            .WhereIf(!string.IsNullOrEmpty(reqQuery.UserId), (a, b) => a.UserId == reqQuery.UserId)
                                                            .WhereIf(!string.IsNullOrEmpty(reqQuery.ProjectId), (a, b) => a.ProjectId == reqQuery.ProjectId);

            var dbres = await where.Count(out var totalCount)
                .Page(reqQuery.PageNo, reqQuery.PageSize)
                .ToListAsync((a, b) => new RepConversationInfo
                {
                    Id = a.Id,
                    ProjectId = b.Id,
                    CreateTime = a.CreateTime,
                    Icon = b.Icon,
                    Name = b.Name,
                    Title = a.Title,
                    ConversationId = a.ConversationId,
                    ChatId = a.ChatId,
                });

            return new Tuple<long, List<RepConversationInfo>>(totalCount, dbres);
        }
    }
}
